package com.wx.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.eachonline.core.JsonResult;
import com.eachonline.util.Md5Util;
import com.eachonline.util.StringUtil;
import com.wx.business.trade.IxbUserService;
import com.wx.business.util.MsgType;
import com.wx.business.util.SmsUtil;
import com.wx.model.IxbUser;


@Controller
@RequestMapping("/user")
public class UserInfoController  {
	
	 
	
	 @Autowired
	 private IxbUserService ixbUserService;
	 
	 /**
      * 登录
      * @param request
      * @param response
      * @param tel
      * @param password
      * @return
      * @throws Exception
      */
	 @RequestMapping(value = "/login", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult login(HttpServletRequest  request ,HttpServletResponse response,String tel,String password) throws Exception {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
		
		if(StringUtil.isEmpty(tel))
			return new JsonResult(false, "手机号不能为空");
		
		
		if(StringUtil.isEmpty(password))
			return new JsonResult(false, "密码不能为空");
		
		
		if(user==null)
			return new JsonResult(false, "请用微信打开注册！");
		
		if(Md5Util.encodeHex(Md5Util.encodeHex(password)).equals(user.getPassword())&&tel.equals(user.getPhone()))
		{
			Map<String,Object> resultMap=new HashMap<String,Object>();
			resultMap.put("useInfo", user);
			return new JsonResult(true, "成功",resultMap);
		}
		 
		if(!Md5Util.encodeHex(Md5Util.encodeHex(password)).equals(user.getPassword()))
		{
			return new JsonResult(false, "密码错误");
		}else{
			return new JsonResult(false, "手机号码");
		}
	
	}

	 
     /**
      * 注册绑定用户
      * @param request
      * @param response
      * @param userId
      * @param tel
      * @param code
      * @param password
      * @return
      * @throws Exception
      */
	 @RequestMapping(value = "/regist", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult useRegist(HttpServletRequest  request ,HttpServletResponse response,String tel,String code,String password) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

		if(StringUtil.isEmpty(code))
		{
			return new JsonResult(false, "短信验证码不能为空");
		}
		
		if(StringUtil.isEmpty(tel))
		{
			return new JsonResult(false, "手机号不能为空");
		}
		if(StringUtil.isEmpty(password))
		{
			return new JsonResult(false, "密码不能为空");
		}
		
		boolean isSucess=SmsUtil.checkCode(code, tel, MsgType.REG_MSG);
		if(!isSucess)
		{
			return new JsonResult(false, "短信验证码不正确");
		}
		
		IxbUser useInfo=ixbUserService.selectByPrimaryKey(user.getSid());
		
		if(useInfo==null)
		{
			return new JsonResult(false, "请用微信打开注册！");
		}
		useInfo.setPassword(Md5Util.encodeHex(Md5Util.encodeHex(password)));
		useInfo.setPhone(tel);
		useInfo.setIsReg(1);
		ixbUserService.updateByPrimaryKeySelective(useInfo);
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("useInfo", useInfo);
		return new JsonResult(true, "成功",resultMap);
        
	}

	
	
      /**
       * 修改用户昵称
       * @param request
       * @param name
       * @param userId
       * @return
       * @throws Exception
       */
	 @RequestMapping(value = "/updateName", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult updateName(HttpServletRequest  request ,String name) throws Exception {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

		if(StringUtil.isEmpty(name))
		{
			return new JsonResult(false, "用户昵称不能为空");
		}
		
		
		IxbUser useInfo=ixbUserService.selectByPrimaryKey(user.getSid());
		
		if(useInfo==null)
		{
			return new JsonResult(false, "请用微信打开注册！");
		}
		useInfo.setUserName(name);
		ixbUserService.updateByPrimaryKeySelective(useInfo);
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("useInfo", useInfo);
		HttpSession  session = request.getSession();
     	session.setAttribute("currentAccount", useInfo);
		return new JsonResult(true, "成功",resultMap);
        
	}

	

     /**
      * 修改密码
      * @param request
      * @param name
      * @param userId
      * @return
      * @throws Exception
      */
	 @RequestMapping(value = "/updatepwd", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult updatepwd(HttpServletRequest  request ,String oldpassword,String newpassword) throws Exception {
		
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

		 
		if(StringUtil.isEmpty(oldpassword))
		{
			return new JsonResult(false, "密码不能为空");
		}
		if(StringUtil.isEmpty(newpassword))
		{
			return new JsonResult(false, "新密码不能为空");
		}
		
		IxbUser useInfo=ixbUserService.selectByPrimaryKey(user.getSid());
		
		if(useInfo==null)
		{
			return new JsonResult(false, "请用微信打开注册！");
		}
		
		if(!Md5Util.encodeHex(Md5Util.encodeHex(oldpassword)).equals(useInfo.getPassword()))
		{
			return new JsonResult(false, "密码输入不正确");
		}
		
		useInfo.setPassword(Md5Util.encodeHex(Md5Util.encodeHex(newpassword)));
		ixbUserService.updateByPrimaryKeySelective(useInfo);
		HttpSession  session = request.getSession();
     	session.setAttribute("currentAccount", useInfo);
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("useInfo", useInfo);
		return new JsonResult(true, "成功",resultMap);
       
	}
	 
	  /**
      * 修改手机号
      * @param request
      * @param name
      * @param userId
      * @return
      * @throws Exception
      */
	 @RequestMapping(value = "/updateTel", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult updateTel(HttpServletRequest  request  ,String tel,String code) throws Exception {
		
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
		if(StringUtil.isEmpty(code))
		{
			return new JsonResult(false, "短信验证码不能为空");
		}
		if(StringUtil.isEmpty(tel))
		{
			return new JsonResult(false, "手机号不能为空");
		}
		boolean isSucess=SmsUtil.checkCode(code, tel, MsgType.UPDATEPHONE_MSG);
		if(!isSucess)
		{
			return new JsonResult(false, "短信验证码不正确");
		}
		IxbUser useInfo=ixbUserService.selectByPrimaryKey(user.getSid());
		
		if(useInfo==null)
		{
			return new JsonResult(false, "请用微信打开注册！");
		}
		
		
		useInfo.setPhone(tel);
		ixbUserService.updateByPrimaryKeySelective(useInfo);
		HttpSession  session = request.getSession();
     	session.setAttribute("currentAccount", useInfo);
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("useInfo", useInfo);
		return new JsonResult(true, "成功",resultMap);
       
	}
	 
	  /**
      * 修改添加家庭住址
      * @param request
      * @param name
      * @param userId
      * @return
      * @throws Exception
      */
	 @RequestMapping(value = "/updateAddress", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult updateAddress(HttpServletRequest  request ,String address) throws Exception {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");

		if(StringUtil.isEmpty(address))
		{
			return new JsonResult(false, "住址不能为空");
		}
		
		IxbUser useInfo=ixbUserService.selectByPrimaryKey(user.getSid());
		
		if(useInfo==null)
		{
			return new JsonResult(false, "请用微信打开注册！");
		}
		
		useInfo.setHomeAddress(address);
		ixbUserService.updateByPrimaryKeySelective(useInfo);
		Map<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("useInfo", useInfo);
		return new JsonResult(true, "成功",resultMap);
       
	}
	 
  
}
